package com.itmeima.text4;

import java.lang.reflect.Field;

public class Demo1 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        Student s = new Student();
        setProperty(s,"name","张三");
        System.out.println(s);

    }

    public static void setProperty(Object obj, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {
        Class c = obj.getClass();

        Field field = c.getDeclaredField(propertyName);

        field.setAccessible(true);

        field.set(obj,value);

    }

}